package com.gao.controller;

import com.gao.entity.User;
import com.gao.util.JwtUtils;
import com.gao.util.ResultVO;
import com.gao.util.ResultVOFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author gao
 * @time 2021/01/28 22:24:30
 */
@RestController
public class AuthController {
    @PostMapping("/auth/login")
    public ResultVO login(@RequestBody User user) {
        try {
            if ("admin".equals(user.getUsername()) && "123".equals(user.getPassword())) {
                String token = JwtUtils.createToken(user.getUsername(), 3600);
                return ResultVOFactory.success(token);
            } else {
                return ResultVOFactory.failure();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVOFactory.failure();
        }
    }
    @PostMapping("/auth/verify")
    public ResultVO verify(@RequestBody String token) {
        try {
            boolean bb = JwtUtils.verifyToken(token);
            if (bb) {
                return ResultVOFactory.success();
            } else {
                return ResultVOFactory.failure();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVOFactory.failure();
        }
    }
}
